package org.sbbs.base.dao.hibernate;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.sbbs.base.dao.GeneralDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.googlecode.genericdao.dao.hibernate.GeneralDAOImpl;

public class GeneralDaoImpl
    extends GeneralDAOImpl
    implements GeneralDao {
    private HibernateTemplate hibernateTemplate;

    private JdbcTemplate jdbcTemplate;

    @Autowired
    @Override
    public void setSessionFactory( SessionFactory sessionFactory ) {
        super.setSessionFactory( sessionFactory );
        this.hibernateTemplate = new HibernateTemplate( sessionFactory );
        this.jdbcTemplate = new JdbcTemplate( SessionFactoryUtils.getDataSource( sessionFactory ) );
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

  /*  public Session getSession() {
        
        
        Session sess = getSessionFactory().getCurrentSession();
        if ( sess == null ) {
            sess = getSessionFactory().openSession();
        }
        return sess;
    }
*/
    @Override
    public <T> boolean exists( Class<T> type, Serializable id ) {
        //T entity = (T) hibernateTemplate.get( type, id );
        T entity = this.find(  type, id );
        return entity != null;
    }
    /*
     * @Override public boolean exists( Serializable id ) { Session sess = getSession(); IdentifierLoadAccess byId =
     * sess.byId( persistentClass ); T entity = (T) byId.load( id ); return entity != null; }
     */

}
